<?php
/**
 * App Module Package (应用模块打包工具)
 * 
 * 用于将用户开发的模块打包成标准的软件安装包
 * 
 * 打包步骤：
 * 1. 打包./Application/{Module}/
 * 
 * @since 1.0 <2015-8-26> SoChishun <14507247@qq.com> Added.
 */
// 程序版本号
$ver = '1.0';

// 定义应用目录
define('APP_PATH', '../Application/XWAM/Addon/');

$am = I('am');
if ('make' == I('action') && $am) {
    make_package($am);
    exit;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>App Module Package - <?php echo $ver ?></title>
        <style type="text/css">body{font-size:12px;}td span{color:#F00;}td div{color:#CCC;}</style>
    </head>
    <body>
        <form method="get">
            <table>
                <tr><th>选择应用模块:</th><td><?php fn_get_ampicker(APP_PATH, $am); ?></td></tr>
                <tr><th>&nbsp;</th><td><button type="submit">生成安装包</button></td></tr>
            </table>
            <input type="hidden" name="action" value="make" />
        </form>
    </body>
</html>
<?php

/**
 * 获取用户传递的参数
 * @param string $name
 * @param string $defv
 * @return string
 * @since 1.0 <2015-8-26> SoChishun Added.
 */
function I($name, $defv = '') {
    if (isset($_POST[$name])) {
        return $_POST[$name];
    }
    if (isset($_GET[$name])) {
        return $_GET[$name];
    }
    return $defv;
}

/**
 * 生成应用模块下拉选项
 * @param string $source App路径
 * @param string $val 默认选中值
 * @since 1.0 <2015-8-27> SoChishun Added.
 */
function fn_get_ampicker($source, $val) {
    if (!is_dir($source)) {
        echo '[APP_PATH]不是有效目录!';
        exit;
    }
    echo '<select name="am">';
    if ($handle = opendir($source)) {
        while (false !== ( $f = readdir($handle) )) {
            if ('.' == $f || '..' == $f) {
                continue;
            }
            if (is_file($source . '/' . $f . '/Install/module.config.php')) {
                echo '<option value="', $f, '"', ($val == $f ? 'selected="selected"' : ''), '>', $f, '</option>';
            }
        }
        closedir($handle);
    }
    echo '</select>';
}

/**
 * 生成安装包
 * @param type $am
 * @since 1.0 <2015-8-28> SoChishun Added.
 */
function make_package($am) {
    $root = APP_PATH . $am;
    preg_match('/P[\d]*/', $am, $matches);
    $config = array(
        'am_name' => $am,
        'skip-dirs' => array(
            $root . '/ProjectResourceLibrary'
        ),
        'skip-files' => array(),
    );
    try {
        $zip = new ZipArchive();
        $msg = $zip->open('./Dist/' . $am . '.zip', ZIPARCHIVE::OVERWRITE);
        if (true === $msg) {
            $zip->setArchiveComment('App Module Install Package' . PHP_EOL . 'Generate at ' . date('Y-m-d H:i:s') . PHP_EOL . 'Powerd by AMPackage.');
            zip_dir($root, $zip, $config);
            $zip->close();
            echo '打包成功! 文件路径[Dist/', $am, '.zip].';
        } else {
            var_export($msg);
        }
    } catch (Exception $ex) {
        echo $ex->getMessage();
    }
}

/**
 * 递归压缩整个目录
 * @param type $source
 * @param type $zip
 * @param type $config
 * @since 1.0 <2015-8-28> SoChishun Added.
 */
function zip_dir($source, &$zip, $config) {
    if (is_dir($source)) {
        if ($handle = opendir($source)) {
            while (false !== ( $f = readdir($handle) )) {
                if ('.' == $f || '..' == $f) {
                    continue;
                }
                $filename = $source . '/' . $f;
                if (is_dir($filename)) {
                    if (!empty($config['skip-dirs']) && in_array($filename, $config['skip-dirs'])) {
                        continue;
                    }
                    zip_dir($filename, $zip, $config);
                } else {
                    if (!empty($config['skip-files']) && in_array($filename, $config['skip-files'])) {
                        continue;
                    }
                    $zip->addFile($filename, substr($filename, strlen(APP_PATH)));
                }
            }
            closedir($handle);
        }
    } else {
        if (!empty($config['skip-files']) && in_array($source, $config['skip-files'])) {
            return;
        }
        $zip->addFile($source);
    }
}
